﻿Imports System.Text.Encoding
Public Class Utilities
    Public Shared Function MaHoaPassword(ByVal password As String) As String
        Dim textBytes As Byte()
        textBytes = System.Text.Encoding.Default.GetBytes(password)
        Dim ret As String = ""
        Try
            Dim cryptHandler As System.Security.Cryptography.MD5CryptoServiceProvider
            cryptHandler = New System.Security.Cryptography.MD5CryptoServiceProvider()
            Dim hash As Byte() = cryptHandler.ComputeHash(textBytes)

            For Each a As Byte In hash
                If (a < 16) Then
                    ret = ret & "0" & a.ToString("x")
                Else
                    ret = ret & a.ToString("x")
                End If
            Next
        Catch ex As Exception
            MessageBox.Show("Không thể mã hóa Password", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
        End Try
        Return ret
    End Function

    Public Shared Function MaHoaPassword2(ByVal password As String) As Byte()
        Dim textBytes As Byte()
        textBytes = System.Text.Encoding.Default.GetBytes(password)
        Dim hash As Byte() = Nothing
        Try
            Dim cryptHandler As System.Security.Cryptography.MD5CryptoServiceProvider
            cryptHandler = New System.Security.Cryptography.MD5CryptoServiceProvider()
            hash = cryptHandler.ComputeHash(textBytes)
        Catch ex As Exception
            MessageBox.Show("Không thể mã hóa Password", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
        End Try
        Return hash
    End Function

    Public Shared Function SoSanhPassword(ByVal p1 As Byte(), ByVal p2 As Byte()) As Boolean
        Dim kq As Boolean = True
        Dim i As Integer = 0
        While (i < p1.Length And i < p2.Length)
            If (p1(i) <> p2(i)) Then
                kq = False
                Exit While
            Else
                i += 1
            End If
        End While
        Return kq
    End Function
End Class
